package com.canaan.business.service.inOutStock.impl;

import java.util.Date;
import java.util.List;
import com.canaan.common.utils.DateUtils;
import com.canaan.common.utils.SecurityUtils;
import com.canaan.common.utils.TableNoUtil;
import com.canaan.common.utils.uuid.UUID;
import com.canaan.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import com.canaan.business.mapper.inOutStock.InStockBillDetailMapper;
import com.canaan.business.domain.inOutStock.InStockBillDetail;
import com.canaan.business.service.inOutStock.IInStockBillDetailService;
import com.canaan.common.annotation.TableName;
/**
 * 入库单明细Service业务层处理
 *
 * @author canaan
 * @date 2025-03-29
 */
@Service
public class InStockBillDetailServiceImpl implements IInStockBillDetailService {
    @Resource
    private InStockBillDetailMapper inStockBillDetailMapper;

    /**
     * 查询入库单明细
     *
     * @param id 入库单明细主键
     * @return 入库单明细
     */
    @Override
    public InStockBillDetail selectInStockBillDetailById(Long id) {
        return inStockBillDetailMapper.selectInStockBillDetailById(id);
    }

    /**
     * 查询入库单明细列表
     *
     * @param inStockBillDetail 入库单明细
     * @return 入库单明细
     */
    @Override
    public List<InStockBillDetail> selectInStockBillDetailList(InStockBillDetail inStockBillDetail) {
        return inStockBillDetailMapper.selectInStockBillDetailList(inStockBillDetail);
    }

    /**
     * 新增入库单明细
     *
     * @param inStockBillDetail 入库单明细
     * @return 结果
     */
    @Override
    public int insertInStockBillDetail(InStockBillDetail inStockBillDetail) {
        if(StringUtils.isBlank(inStockBillDetail.getNo())){
            inStockBillDetail.setNo(TableNoUtil.getNo(InStockBillDetail.class));
        }
        inStockBillDetail.setCreateBy(SecurityUtils.getUsername());
        inStockBillDetail.setCreateTime(DateUtils.getNowDate());
        return inStockBillDetailMapper.insertInStockBillDetail(inStockBillDetail);
    }

    /**
     * 批量新增入库单明细
     *
     * @param inStockBillDetailList 入库单明细列表
     * @return 结果
     */
    @Override
    public int batchInsertInStockBillDetail(List<InStockBillDetail> inStockBillDetailList) {
        String username = SecurityUtils.getUsername();
        Date currerTime = DateUtils.getNowDate();
        for (InStockBillDetail inStockBillDetail : inStockBillDetailList){
            if(StringUtils.isBlank(inStockBillDetail.getNo())){
                inStockBillDetail.setNo(TableNoUtil.getNo(InStockBillDetail.class));
            }
            inStockBillDetail.setCreateBy(username);
            inStockBillDetail.setCreateTime(currerTime);
        }
        int insertNum = 0;
        for (int i=0; i<inStockBillDetailList.size();) {
            int endIndex = i+500;
            if (endIndex > inStockBillDetailList.size()) {
                endIndex = inStockBillDetailList.size();
            }
            insertNum = insertNum + inStockBillDetailMapper.batchInsertInStockBillDetail(inStockBillDetailList.subList(i, endIndex));
            i = endIndex;
        }
        return insertNum;
    }

    /**
     * 修改入库单明细
     *
     * @param inStockBillDetail 入库单明细
     * @return 结果
     */
    @Override
    public int updateInStockBillDetail(InStockBillDetail inStockBillDetail) {
        inStockBillDetail.setUpdateBy(SecurityUtils.getUsername());
        inStockBillDetail.setUpdateTime(DateUtils.getNowDate());
        return inStockBillDetailMapper.updateInStockBillDetail(inStockBillDetail);
    }

    /**
     * 批量修改入库单明细
     *
     * @param inStockBillDetailList 入库单明细列表
     * @return 结果
     */
    @Override
    public int batchUpdateInStockBillDetail(List<InStockBillDetail> inStockBillDetailList) {
        String username = SecurityUtils.getUsername();
        Date currerTime = DateUtils.getNowDate();
        for (InStockBillDetail inStockBillDetail : inStockBillDetailList){
            inStockBillDetail.setUpdateBy(username);
            inStockBillDetail.setUpdateTime(currerTime);
        }
        int updateNum = 0;
        for (int i=0; i<inStockBillDetailList.size();) {
            int endIndex = i+500;
            if (endIndex > inStockBillDetailList.size()) {
                endIndex = inStockBillDetailList.size();
            }
            updateNum = updateNum + inStockBillDetailMapper.batchUpdateInStockBillDetail(inStockBillDetailList.subList(i, endIndex));
            i = endIndex;
        }
        return updateNum;
    }

    /**
     * 批量删除入库单明细
     *
     * @param ids 需要删除的入库单明细主键集合
     * @return 结果
     */
    @Override
    public int deleteInStockBillDetailByIds(Long[] ids) {
        return inStockBillDetailMapper.deleteInStockBillDetailByIds(ids);
    }

    /**
     * 删除入库单明细信息
     *
     * @param id 入库单明细主键
     * @return 结果
     */
    @Override
    public int deleteInStockBillDetailById(Long id) {
        return inStockBillDetailMapper.deleteInStockBillDetailById(id);
    }

    /**
     * 查询入库单明细
     *
     * @param no 编号
     * @return 入库单明细
     */
    @Override
    public InStockBillDetail selectInStockBillDetailByNo(String no) {
        return inStockBillDetailMapper.selectInStockBillDetailByNo(no);
    }

    /**
     * 批量删除入库单明细
     *
     * @param nos 需要删除的编号集合
     * @return 结果
     */
    @Override
    public int deleteInStockBillDetailByNos(String[] nos) {
        return inStockBillDetailMapper.deleteInStockBillDetailByNos(nos);
    }

    /**
     * 删除入库单明细信息
     *
     * @param no 编号
     * @return 结果
     */
    @Override
    public int deleteInStockBillDetailByNo(String no) {
        return inStockBillDetailMapper.deleteInStockBillDetailByNo(no);
    }
}
